<script>
export default {
    name:'Login',
    data(){
        const validatePassword = (rule,value,callback)=>{
            if(value.length < 6){
                callback(new Error('The password can not be less 6 digits'))
            }else{
                callback()
            }
        }
        return{
            loginForm:{
                username:'xxxx',
                password:'xxxx'
            },
            loginRules:{
                username:[
                    { required: true, trigger: 'blur'}
                ],
                password:[
                    { required: true, trigger:'blur',validator:validatePassword}
                ]
            },
            loading:false,
            passwordType:'password',
            redirect:undefined
        }
    },
    watch:{
        $route:{
            handler:function(route){
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        showPwd(){
            if(this.passwordType === 'password'){
                this.passwordType = ''
            }else{
                this.passwordType = 'password'
            }
            this.$nextTick(()=>{
                this.$refs.password.focus()
            })
        },
        handleLogin(){
            loginApi.doLogin({
                ...this.loginForm
            })
            .then((res)=>{
                if(res.success){
                    var token = res.data.token
                    var info = res.data.userInfo

                    setToken(token)
                    setUserInfo(info)
                    this.$router.push({ path: '/'})
                }else{
                    this.$message.error('登录失败!')
                }
            })
        }
    },
}
</script>